package dev.helper;

import java.util.List;

import eu.news.data.hibernate.dao.HB_OperatorDAO;
import eu.news.data.hibernate.dao.HB_SortDAO;
import eu.news.data.hibernate.dao.HB_SpecialDAO;
import eu.news.data.pojo.Operator;
import eu.news.data.pojo.Sort;
import eu.news.data.pojo.Special;

public class DeployInitialize {
	
	public static void main(String [] args){
		updateOperator(updateSort(updateSpecial()));
	}
	
	public static Special updateSpecial(){
		HB_SpecialDAO spDao=new HB_SpecialDAO();
		if(spDao.getByName("default")!=null)
			return spDao.getByName("default");
		Special ap=new Special("新闻");
		ap.setTheme("default");
		spDao.save(ap);
		spDao.commit();
		return ap;
	}
	
	public static List<Sort> updateSort(Special sp){
		HB_SortDAO dao=new HB_SortDAO();
		String[] names={"校内新闻",
				"通知公告",
				"讲座信息",
				"人物访谈",
				"专题报导",
				"前沿知识",
				"他山之石"};
		
		for(String name:names){
			Sort sort=dao.getByNameSpecial(name,sp);
			if(sort==null){
				sort=new Sort(name, sp);
				dao.save(sort);
			}
		}
		dao.commit();
		return dao.getBySpecial(sp);
	}
	
	public static void updateOperator(List<Sort> sorts){
		HB_OperatorDAO dao=new HB_OperatorDAO();
		Operator op=new Operator("调试用户", Operator.TYPE.LOCAL_ADMIN);
		op.setSorts(sorts);
		dao.saveOrUpdate(op);
		dao.commit();
	}
	
}
